<?php
Yii::import('common.models.resource.*');
class ResourceController extends Controller{
    public $mode; //iframe or normal
    public function __construct($id, $module = null) {
        parent::__construct($id, $module);

        //if parent call iframe set iframe layout
        if(isset($_GET['mode']) && 'iframe' == $_GET['mode']){
            $this->layout = 'resource_iframe';
            $this->mode = 'iframe';
        }else{
            $this->mode = 'normal';
        }
    }

    /**
     * adding default actions
     * @return type
     */
    public function actions() {
        return array(
            //handle upload form using xupload
            'upload' => array(
                'class' => 'application.components.action.ResourceUploadAction',
                'path' => UPLOAD_PATH,
                'publicPath' => UPLOAD_URL,
            ),
        );
    }

    public function actionCreate(){
        $resourceModel = new Resource;
        $isExternal = FALSE;
        if(isset($_POST['Resource'])){
            $resourceModel->attributes = $_POST['Resource'];

            $isExternal = TRUE;
            //save
            if($resourceModel->save()){
                user()->setFlash('success', 'Upload media successfully!');

                $this->refresh();
            }
        }

        Yii::import("common.extensions.xupload.models.XUploadForm");
        $xuploadModel = new XUploadForm;

        $this->render('create', array(
            'resource' => $resourceModel,
            'xupload' => $xuploadModel,
            'isExternal' => $isExternal
        ));
    }

    public function actionUpdate($id){
        $model = Resource::model()->findByPk($id);

        if(!$model){
            throw new CHttpException(404, 'Page has not exists!');
        }

        if(isset($_POST['Resource'])){
            $model->attributes = $_POST['Resource'];

            if($model->save()){
                user()->setFlash('success', 'Resource has been updated!');
            }
        }

        if($this->mode == 'iframe'){
            $this->render('_form', array(
                'model' => $model
            ));
        }else{
            $this->render('update', array(
                'model' => $model
            ));
        }

    }

    /**
     * list all resource
     */
    public function actionIndex(){
        if(r()->isPostRequest){
            //bunk actions
            if(isset($_POST['ids']) && is_array($_POST['ids'])){
                $models = Resource::model()->findAll('resource_id IN ('.  implode($_POST['ids'], ',').')');
                foreach($models as $model){
                    $model->delete();
                }
            }
        }

        $model = new Resource('search');
        if(isset($_GET['Resource']))
            $model->attributes = $_GET['Resource'];

        $this->render('index', array(
            'model' => $model
        ));
    }

    public function actionDelete($id){
        if(r()->isPostRequest){
            Resource::model()->findByPk($id)->delete();

            if (!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }else{
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
}